const express = require('express');
const router = express.Router();
const db = require('../../../util/dbConfig/dbConfig')

router.post('/',async (req,res) => {
    let {sheepId} = req.body
    //如果没有sheepId值
    if(!sheepId){
        return res.send({
            code: 1,
            msg: '参数错误'
        })
    }
    let userId = req.session.userInfo._id  //登录用户的id
    let admin = req.session.userInfo.admin  //登录的该用户是否为管理员
    //如果有
    try{
        let sqlOutside,sqlBaseInfo,sqlFamily,sqlTest
        let sqlArr = []
        if(admin === 0){
            //非管理员
            sqlOutside = `select 
d.id 个体名称,
d.GARDEN_NAME 园区名称,
d.PICTURE 图片,
e.name 户主
from sheep d,user e
where d.user_id=e.id
and d.id=?
and e.id=?`
            sqlBaseInfo = `select 
            
a.ID 个体号,
a.VARIETY 品种,
case a.sex when 1 then '公羊' else '母羊' end as '性别',
a.BIRTHDAY 出生日期,
a.LEVEL 级别,
TIMESTAMPDIFF(year,a.birthday,now()) 年龄,
a.BIRTH_WEIGHT 出生体重,
a.DIE 死亡,
a.DIE_DATE 死亡日期,
a.SOLD_ID 订单编号,
a.WEANING_WEIGHT 断奶体重
from sheep a,user b
where a.id=?
and b.id=?
and a.user_id=b.id`
            sqlFamily = `select
b.id 个体号,
b.fu 父号,
b.mu 母号,
b.zufu 祖父号,
b.zumu 祖母号,
b.waizufu 外祖父号,
b.waizumu 外祖母号
from sheep b,user c
where b.id=?
and c.id=?
and b.user_id=c.id`
            sqlTest = `select
b.id 个体编号,
c.YEAR 年度,
c.BODYTALL 体高,
c.BODYLONG 体长,
c.CHEST 胸围,
c.WEIGHT 体重,
c.LENGTH 绒长度,
c.FINE 绒细度,
c.RONGYIELD 绒产量,
c.TANK 等级,
c.REMARK 备注
from production c,sheep b,user a
where c.YEAR>DATE_SUB(CURDATE(),INTERVAL 3 YEAR)
and b.id=c.sheep_id
and b.user_id=a.id
and a.id=?
and b.id=?`
            sqlArr = [userId,sheepId]
        }else if(admin === 1){
            //管理员
            sqlOutside = `select 
d.id 个体名称,
d.GARDEN_NAME 园区名称,
d.PICTURE 图片,
e.name 户主
from sheep d,user e
where d.user_id=e.id
and d.id=?`
            sqlBaseInfo = `select 
a.ID 个体号,
a.VARIETY 品种,
case a.sex when 1 then '公羊' else '母羊' end as '性别',
a.BIRTHDAY 出生日期,
a.LEVEL 级别,
TIMESTAMPDIFF(year,a.birthday,now()) 年龄,
a.BIRTH_WEIGHT 出生体重,
a.DIE 死亡,
a.DIE_DATE 死亡日期,
a.SOLD_ID 订单编号,
a.WEANING_WEIGHT 断奶体重
from sheep a
where a.id=?`
            sqlFamily = `select
b.id 个体号,
b.fu 父号,
b.mu 母号,
b.zufu 祖父号,
b.zumu 祖母号,
b.waizufu 外祖父号,
b.waizumu 外祖母号
from sheep b
where b.id=?`
            sqlTest = `select
b.id 个体编号,
c.YEAR 年度,
c.BODYTALL 体高,
c.BODYLONG 体长,
c.CHEST 胸围,
c.WEIGHT 体重,
c.LENGTH 绒长度,
c.FINE 绒细度,
c.RONGYIELD 绒产量,
c.TANK 等级,
c.REMARK 备注
from production c,sheep b,user a
where c.YEAR>DATE_SUB(CURDATE(),INTERVAL 3 YEAR)
and b.id=c.sheep_id
and b.user_id=a.id
and b.id=?`
            sqlArr = [sheepId]
        }
        let data1 = await db.SySqlConnect(sqlOutside,sqlArr)  //获取卡片外信息
        let data2 = await db.SySqlConnect(sqlBaseInfo,sqlArr)  //获取卡片羊只基本信息
        let data3 = await db.SySqlConnect(sqlFamily,sqlArr)  //获取卡片系谱档案信息
        let data4 = await db.SySqlConnect(sqlTest,sqlArr)  //获取卡片生产性能信息
        if(!data1 || data1.length === 0){
            return res.send({
                code: 1,
                msg: '无卡片园区信息'
            })
        }
        if(!data2 || data2.length === 0){
            return res.send({
                code: 1,
                msg: '无卡片羊只基本信息'
            })
        }
        if(!data3 || data3.length === 0){
            return res.send({
                code: 1,
                msg: '无卡片系谱档案信息'
            })
        }
        if(!data4 || data4.length === 0){
            return res.send({
                code: 1,
                msg: '无卡片卡片生产性能信息'
            })
        }
        res.send({
            code: 0,
            msg: '加载成功',
            data: {
                data1,
                data2,
                data3,
                data4
            }
        })

    }catch (e) {
        console.log(e);
        return res.send({
            code: 4,
            msg: '服务器错误，请稍后再试'
        })
    }
})

module.exports = router
